
/*
 * \class
 * \brief This class validates the user registration content on the client side
 * \author Salman Noor
 */

$(function(){




        var firstName = jQuery(':input[id=firstName]');
        var lastName = jQuery(':input[id=lastName]');
        var userName = jQuery(':input[id=userName]');
        var formEmail = jQuery(':input[id=emailAddress]');
        var formPassword = jQuery(':input[id=passwordField]');
        var formRepeatPassword = jQuery(':input[id=repeatPasswordField]');
           // var userTypeButton = jQuery(':input[name=userType]');


            $('#container_userType').buttonset();
            //jQuery('.ui-button-text').css('width','250px');

var hiddenResponseDiv = jQuery('.responseDiv');
hiddenResponseDiv.hide();
        var allFields = jQuery([]).add(firstName).add(lastName).add(userName).add(formEmail).add(formPassword).add(formRepeatPassword);

$(".registerButton").button();

                    $(".registerButton").button({

                        icons: {
                            primary: 'ui-icon-comment'
                        },
                        text: true
                    });

                   $("#dialog-box-registration").dialog({
            resizable: true,
            width: 540,
            modal: true,
            show: 'clip',
            hide: 'clip',
                        autoOpen: false,
            closeOnEscape: false,
            buttons: {
              'Register': function() {

                },
                Cancel: function() {

                }
            }
        });

$(".registerButton").click(function(){

     jQuery( "#dialog-box-registration").dialog({ title: 'Register as a new User' });
    $("#dialog-box-registration").dialog('open');
                        $("#dialog-box-registration").dialog( "option", "buttons", {
                            "Register": function() {
                                // jQuery(this).dialog("close");
                               var bValid = true;
                    allFields.removeClass('ui-state-error');

                   bValid = bValid && formValidator.checkLength(firstName,"your first name",2,20);
                   bValid = bValid && formValidator.checkRegexp(firstName,/^([0-9a-zA-Z])+$/,"The first name field only allows alphanumeric characters (a-z 0-9).");
                   bValid = bValid && formValidator.checkLength(lastName,"your last name",2,20);
                   bValid = bValid && formValidator.checkRegexp(lastName,/^([0-9a-zA-Z])+$/,"The last name field only allows alphanumeric characters (a-z 0-9).");
                   bValid = bValid && formValidator.checkRegexp(formEmail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Enter a valid email address, eg. yourname@wits.ac.za.");
                   bValid = bValid && formValidator.checkLength(userName,"your user name",1,20);
                   bValid = bValid && formValidator.checkRegexp(userName,/^([0-9a-zA-Z])+$/,"The user name field only allows alphanumeric characters (a-z 0-9).");
                    bValid = bValid && formValidator.checkLength(formPassword,"your password",1,20);
                   bValid = bValid && formValidator.checkRegexp(formPassword,/^([0-9a-zA-Z])+$/,"The password field only allows alphanumeric characters (a-z 0-9).");
                    bValid = bValid && formValidator.checkLength(formRepeatPassword,"your password",1,20);
                   bValid = bValid && formValidator.checkRegexp(formRepeatPassword,/^([0-9a-zA-Z])+$/,"The password field only allows alphanumeric characters (a-z 0-9).");
                  bValid = bValid && formValidator.checkPasswordFields(formPassword,formRepeatPassword);

                    if (bValid) {




sendRequest.setAction('addNewUser');

var userType1 = $("input:radio[name=userType]:checked").val();
formData = {'firstName':firstName.val(),'lastName':lastName.val(),'formEmail':formEmail.val(),'userName':userName.val(),'formPassword':formPassword.val(),'userType': userType1}
sendRequest.setSendParameters(formData);
//sendRequest.sentInfo();
hiddenResponseDiv.load(sendRequest.serverURL, sendRequest.getSendData(), function(){
hiddenResponseDiv.unbind();
if (hiddenResponseDiv.html() == '1')
    {
       jQuery("#dialog-box-registration").dialog('close');
 jQuery('.errorMessageDiv').html('');
       jQuery("#dialogue-registrationOKMessage").dialog('open');
                         jQuery('input[id=firstName]').val('');
jQuery('input[id=lastName]').val('');
    jQuery('input[id=userName]').val('');
        jQuery('input[id=emailAddress]').val('');
               jQuery('input[id=passwordField]').val('');
     jQuery('input[id=repeatPasswordField]').val('');
    }
    else
        {
 userName.addClass('ui-state-error');
formValidator.updateErrorMessage("User Name Already Exists. Please Select another User Name");
        }
});
                    }
                            },  "Cancel": function() {
                                                        jQuery(this).dialog('close');
                jQuery('.errorMessageDiv').html('');
       jQuery('input[id=firstName]').val('');
jQuery('input[id=lastName]').val('');
    jQuery('input[id=userName]').val('');
        jQuery('input[id=emailAddress]').val('');
        jQuery('input[id=passwordField]').val('');
     jQuery('input[id=repeatPasswordField]').val('');
                            }
                            //jQuery(this).dialog("close");
                        });

})


        jQuery("#dialogue-registrationOKMessage").dialog({
            resizable: true,
            width:400,
            autoOpen: false,
            modal: true,
            hide: 'puff',
            show: 'puff',
            buttons: {
                'OK': function() {
                    jQuery(this).dialog('close');
                }
            }
        });


});